import { history, useRequest } from '@umijs/max'
import './index.less'
import { applyPage, userLogPage } from './service'

interface RecordProps {
    height?: number | string
    type: 'apply' | 'search'
}

const Record: React.FC<RecordProps> = ({
    height,
    type,
}) => {

    const { data } = useRequest(() => {
        const api = type == 'apply' ? applyPage : userLogPage
        return api({
            page: 1,
            size: 100
        })
    })

    const list = data?.current_data?.map((item: any) => {
        const newItem = {
            ...item,
        }
        if (type === 'apply') {
            newItem.title = item.projectName + '-' + item.companyName + '-' + item.contacts
        } else if (type === 'search') {
            newItem.title = (item.region||'') +'-'+item.deviceType +'-'+item.searchValue
        }

        return newItem
    })
    return <div style={{ height }} className="record_wrap">

        {list?.map((item: any, index: number) => {
            return <a key={item.id}
            onClick={() => {
                if(type == 'apply') {
                    history.push('/apply')
                } else {
                    history.push('/user-log')
                }
                
            }}
            className='record_wrap_item'>

                <div className='record_wrap_item_left'>
                    {index + 1}
                </div>
                <div className='record_wrap_item_right'>
                    {item.title}
                </div>
            </a>
        })}

    </div>
}

export default Record